import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Data from '../views/Data.vue'
import User from '../views/User.vue'
import UserAdd from '../views/UserAdd.vue'
import Login from '../views/Login.vue'
import DataAdmin from '../views/DataAdmin.vue'
import DataOld from '../views/DataOld.vue'
import DataUser from '../views/DataUser.vue'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    redirect:'/data',
    children: [
      {
        path: '/data',
        name: 'Data',
        component:Data
      },
      {
        path: '/dataUser',
        name: 'DataUser',
        component:DataUser
      },
      {
        path: '/dataOld',
        name: 'DataOld',
        component:DataOld
      },
     

      {
        path: '/user',
        name: 'User',
        component:User
      },
      {
        path: '/userAdd',
        name: 'UserAdd',
        component:UserAdd
      },
      {
        path: '/dataAdmin',
        name: 'DataAdmin',
        component:DataAdmin
      },
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component:Login
  }
]

const router = new VueRouter({
  routes
})
//路由导航守卫
//to将有访问的路径
//from从哪儿来
//next是函数
//next()放行   next('/login')跳转
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  //判断token是否存在
  const token = window.sessionStorage.getItem('token')
  if (!token) return next('/login')
  next()
})
export default router
